package com.five.wechat.serviceImp;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.five.common.been.ReceiveMenuEntity;
import com.five.common.been.ReceiveSubscribeEntity;
import com.five.common.been.enumbeen.Event_Type;
import com.five.wechat.service.EventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @ClassName EventServiceImp
 * @Author wuxuan
 * @Date 2019/1/19 13:19
 * @Version 1.0
 * @Description 处理event类型
 **/
@Service("eventServiceType")
@Slf4j
public class EventServiceImp implements EventService {

    /**
     * @MethodName handleAllEvent
     * @Data 2019/1/19 13:21
     * @Author wuxuan
     * @Description
     **/
    ObjectMapper mapper = new ObjectMapper();

    @Override
    public String handleAllEvent(Map<String, String> xml) throws IOException {
        String type = xml.get("Event");
        String xmlMsg = null;
        String json = mapper.writeValueAsString(xml);
        if (type.equals(Event_Type.SUBSCRIBE.toString())) {
            ReceiveSubscribeEntity subscribeEntity = mapper.readValue(json, ReceiveSubscribeEntity.class);
            xmlMsg = subscribeEvent(subscribeEntity);
        } else if (type.equals(Event_Type.UNSUBSCRIBE.toString())) {
            ReceiveSubscribeEntity subscribeEntity = mapper.readValue(json, ReceiveSubscribeEntity.class);
            xmlMsg = unsubscribeEvent(subscribeEntity);
        } else if (type.equals(Event_Type.SCANLOCATION.toString())) {

        } else if (type.equals(Event_Type.CLICK.toString())) {
            ReceiveMenuEntity menuEntity = mapper.readValue(json, ReceiveMenuEntity.class);
            xmlMsg = clickEvent(menuEntity);
        } else if (type.equals(Event_Type.VIEW.toString())) {
            ReceiveMenuEntity menuEntity = mapper.readValue(json, ReceiveMenuEntity.class);
            xmlMsg = viewEvent(menuEntity);
        } else {
            xmlMsg = otherEvent(xml);
        }
        return xmlMsg;
    }

    private String subscribeEvent(ReceiveSubscribeEntity entity) {
        return null;
    }

    private String unsubscribeEvent(ReceiveSubscribeEntity entity) {
        return null;
    }

    private String clickEvent(ReceiveMenuEntity entity) {
        return null;
    }

    private String viewEvent(ReceiveMenuEntity entity) {
        return null;
    }

    private String otherEvent(Map<String, String> xml) {
        log.info("产生了一次未匹配的事件");
        return null;
    }

}
